<template>
  <div class="process-editor">
    <vuedraggable :style="{ width: iWidth, height: iHeight }" :move="checkMove" v-model="_list">
      <transition-group tag="div" class="transition-wraper">
        <div class="item-wraper" v-for="(item, key) of _list" :key="'a-' + (item.id || key)">
          <div :key="'item' + (item.id || key)" class="item-content" @click="handleClick(item)">
            <div v-if="editable(item)" class="control">
              <el-link icon="el-icon-close" type="danger" :underline="false" @click="delItem(item, key)"></el-link>
            </div>
            <span>{{ item.name }}</span>
          </div>
          <div
            v-if="item.code !== 90"
            :key="'sep' + (item.id || key)"
            class="item-sep__arrow"
            :class="{ 'can-add': checkAdd(item) }"
          >
            <i class="el-icon-d-arrow-right"></i>
            <el-link
              v-if="checkAdd(item)"
              icon="el-icon-circle-plus-outline"
              :underline="false"
              @click="addItem(item, key)"
            ></el-link>
          </div>
        </div>
      </transition-group>
    </vuedraggable>
    <el-form :model="currentItem" :inline="true" class="item-form" v-show="showForm">
      <el-form-item label="流程名称">
        <el-select
          v-model="currentItem.code"
          placeholder="流程名称"
          class="item-name-control"
          :disabled="!editable(currentItem)"
          @change="handleItemNameChage"
        >
          <el-option
            v-for="option of itemOptions(currentItem)"
            :key="option.code"
            :value="option.code"
            :label="option.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等待时间">
        <el-input-number
          v-model="currentItem.waitTime"
          placeholder="等待时间"
          controls-position="right"
          :min="-1"
          :step="30"
        ></el-input-number>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// https://gitee.com/xiaoka2017/easy-flow/tree/master

// item 拖动,可以改变流程的顺序, hover 显示删除按钮 和 修改类型的下拉选框
// spe hover,显示添加流程的按钮,无拖动事件
import vuedraggable from 'vuedraggable'

const processItem = Object.freeze({
  Option30: { name: '工厂质检', code: 30 },
  Option40: { name: '工厂验收', code: 40 },
  Option50: { name: '平台质检', code: 50 },
  Option60: { name: '平台验收', code: 60 },
  Option70: { name: '客户质检', code: 70 },
  Option80: { name: '客户验收', code: 80 },
})
export default {
  name: 'ProcessEditor',
  components: { vuedraggable },
  props: {
    width: {
      type: [String, Number],
      default: '100%',
    },
    height: {
      type: [String, Number],
      default: 200,
    },
    list: {
      type: Array,
    },
  },
  model: {
    prop: 'list',
    event: 'change',
  },
  computed: {
    iWidth() {
      return this.width instanceof String ? this.width : this.width + 'px'
    },
    iHeight() {
      return this.height instanceof String ? this.height : this.height + 'px'
    },
    _list: {
      get() {
        if (!this.list || this.list.length === 0) {
          this.$emit('change', this.items)
          return this.items
        }
        return [...this.list]
      },
      set(val) {
        this.$emit('change', val)
      },
    },
  },
  data() {
    return {
      // 默认
      items: [
        { name: '新建', code: 0 },
        { name: '分包', code: 10 },
        { name: '标注', code: 20 },
        { name: '完成', code: 90 },
      ],
      currentItem: {},
      showForm: false,
    }
  },
  methods: {
    checkMove(evt, originalEvent) {
      return this.editable(evt.draggedContext.element) && this.editable(evt.relatedContext.element)
    },

    editable(item) {
      return item.code > 20 && item.code < 90
    },

    checkAdd(item) {
      return item.code > 10
    },

    addItem(item, key) {
      const items = this._list
      if (item.code < 20) {
        return
      }
      const newItem = processItem['Option' + (item.code + 10)]
      if (newItem) {
        items.splice(key + 1, 0, { ...newItem })
        this._list = items
      }
    },

    delItem(item, key) {
      const items = this._list
      items.splice(key, 1)
      this._list = items
    },

    handleItemNameChage(code) {
      this.currentItem.name = processItem['Option' + code].name
    },

    handleClick(item) {
      this.currentItem = item
      this.showForm = true
    },

    itemOptions(item) {
      if (this.editable(item)) {
        return Object.values(processItem)
      } else {
        return [item]
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.process-editor {
  padding: 10px;

  .transition-wraper {
    width: 100%;
    height: 100%;
    display: -webkit-flex;
    display: flex;
    background: #f3f3f3;
    padding: 10px;

    flex-flow: row nowrap;
    justify-content: space-between;
    align-items: stretch;

    .item-wraper {
      text-align: center;
      display: flex;
      align-items: stretch;
      flex-grow: 1;

      &:last-child {
        flex-grow: 0;
      }

      .item-content {
        border-radius: 5px;
        background: #fff;
        padding: 20px 5px;
        display: flex;
        flex-flow: column nowrap;
        justify-content: center;
        position: relative;

        &:active {
          color: red;
        }
        &:hover .control {
          display: block;
        }

        > .control {
          position: absolute;
          top: 10px;
          left: 0;
          width: 100%;
          padding: 0 10px;
          text-align: right;
          display: none;
        }
      }
    }
  }

  .item-form {
    margin-top: 10px;

    .item-name-control {
      /deep/ .el-input.is-disabled .el-input__inner {
        color: #606266;
        background: #fff;
      }
    }
  }
}

.item-sep__arrow {
  flex-grow: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  padding: 0 10px;

  &:hover {
    .el-link {
      display: inline-flex;
    }
  }
  &.can-add:hover {
    > i {
      visibility: hidden;
    }
  }

  .el-link {
    position: absolute;
    display: none;
    font-size: 30px;
  }
}
</style>
